<!DOCTYPE html>



  


<html class="theme-next gemini use-motion" lang="zh-Hans">
<head><meta name="generator" content="Hexo 3.8.0">
  <meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform">
<meta http-equiv="Cache-Control" content="no-siteapp">
















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css">







<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css">

<link href="/css/main.css?v=5.1.4" rel="stylesheet" type="text/css">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="Qtcreator,实战,">










<meta name="description" content="Qt实战">
<meta name="keywords" content="Qtcreator,实战">
<meta property="og:type" content="article">
<meta property="og:title" content="Qt实战（1）">
<meta property="og:url" content="http://Anzusen.com/2019/05/06/Qt实战（1）/index.html">
<meta property="og:site_name" content="Anzhusen">
<meta property="og:description" content="Qt实战">
<meta property="og:locale" content="zh-Hans">
<meta property="og:image" content="http://anzusen.com/image/mywindow.png">
<meta property="og:updated_time" content="2020-04-06T13:13:32.612Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Qt实战（1）">
<meta name="twitter:description" content="Qt实战">
<meta name="twitter:image" content="http://anzusen.com/image/mywindow.png">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Gemini',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://Anzusen.com/2019/05/06/Qt实战（1）/">





  <title>Qt实战（1） | Anzhusen</title>
  








</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>
    <a href="https://github.com/ananzhusen/ananzhusen.github.io" class="github-corner" aria-label="View source on GitHub"><svg width="80" height="80" viewbox="0 0 250 250" style="fill:#64CEAA; color:#fff; position: absolute; top: 0; border: 0; right: 0;" aria-hidden="true"><path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"/><path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"/><path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"/></svg></a><style>.github-corner:hover .octo-arm{animation:octocat-wave 560ms ease-in-out}@keyframes octocat-wave{0%,100%{transform:rotate(0)}20%,60%{transform:rotate(-25deg)}40%,80%{transform:rotate(10deg)}}@media (max-width:500px){.github-corner:hover .octo-arm{animation:none}.github-corner .octo-arm{animation:octocat-wave 560ms ease-in-out}}</style>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">Anzhusen</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br>
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br>
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br>
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br>
            
            归档
          </a>
        </li>
      

      
        <li class="menu-item menu-item-search">
          
            <a href="javascript:;" class="popup-trigger">
          
            
              <i class="menu-item-icon fa fa-search fa-fw"></i> <br>
            
            搜索
          </a>
        </li>
      
    </ul>
  

  
    <div class="site-search">
      
  <div class="popup search-popup local-search-popup">
  <div class="local-search-header clearfix">
    <span class="search-icon">
      <i class="fa fa-search"></i>
    </span>
    <span class="popup-btn-close">
      <i class="fa fa-times-circle"></i>
    </span>
    <div class="local-search-input-wrapper">
      <input autocomplete="off" placeholder="搜索..." spellcheck="false" type="text" id="local-search-input">
    </div>
  </div>
  <div id="local-search-result"></div>
</div>



    </div>
  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://Anzusen.com/2019/05/06/Qt实战（1）/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="ananzhusen">
      <meta itemprop="description" content>
      <meta itemprop="image" content="/images/touxiang.JPG">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Anzhusen">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">Qt实战（1）</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2019-05-06T19:59:55+08:00">
                2019-05-06
              </time>
            

            

            
          </span>

          
            <span class="post-category">
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/QT界面/" itemprop="url" rel="index">
                    <span itemprop="name">QT界面</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
              <span class="post-comments-count">
                <span class="post-meta-divider">|</span>
                <span class="post-meta-item-icon">
                  <i class="fa fa-comment-o"></i>
                </span>
                <a href="/2019/05/06/Qt实战（1）/#comments" itemprop="discussionUrl">
                  <span class="post-comments-count valine-comment-count" data-xid="/2019/05/06/Qt实战（1）/" itemprop="commentCount"></span>
                </a>
              </span>
            
          

          
          

          
            <span class="post-meta-divider">|</span>
            <span class="page-pv">本文总阅读量 <i class="fa fa-file-o"></i>
            <span class="busuanzi-value" id="busuanzi_value_page_pv"></span>次
            </span>
          

          
            <div class="post-wordcount">
              
                
                <span class="post-meta-item-icon">
                  <i class="fa fa-file-word-o"></i>
                </span>
                
                  <span class="post-meta-item-text">字数统计&#58;</span>
                
                <span title="字数统计">
                  2.2k
                </span>
              

              
                <span class="post-meta-divider">|</span>
              

              
                <span class="post-meta-item-icon">
                  <i class="fa fa-clock-o"></i>
                </span>
                
                  <span class="post-meta-item-text">阅读时长 &asymp;</span>
                
                <span title="阅读时长">
                  9
                </span>
              
            </div>
          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <h1 id="Qt实战"><a href="#Qt实战" class="headerlink" title="Qt实战"></a>Qt实战</h1><a id="more"></a>
<h2 id="通过两个独立窗口的切换来学习信号和槽"><a href="#通过两个独立窗口的切换来学习信号和槽" class="headerlink" title="通过两个独立窗口的切换来学习信号和槽"></a>通过两个独立窗口的切换来学习信号和槽</h2><h3 id="创建项目"><a href="#创建项目" class="headerlink" title="创建项目"></a>创建项目</h3><p>  新建Qt Widgets应用,项目名称SingelAndSlot ,类名默认为mainWidget,基类默认为QWidget,不勾选创建界面。</p>
<ol>
<li><p>先来认识SingelAndSlot.pro文件：</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">#添加模块</span><br><span class="line">QT       += core gui</span><br><span class="line"></span><br><span class="line">#高于<span class="number">4</span>版本，添加QT += widgets，为了兼容Qt4</span><br><span class="line">greaterThan(QT_MAJOR_VERSION, <span class="number">4</span>): QT += widgets</span><br><span class="line"></span><br><span class="line">#应用程序的名字</span><br><span class="line">TARGET = SingelAndSlot</span><br><span class="line">#指定SingelAndSlot的类型为app</span><br><span class="line">TEMPLATE = app</span><br><span class="line"></span><br><span class="line">#源文件 .cpp文件</span><br><span class="line">SOURCES += main.cpp\</span><br><span class="line">        mainwidget.cpp</span><br><span class="line"></span><br><span class="line">#头文件 .h文件</span><br><span class="line">HEADERS  += mainwidget.h</span><br></pre></td></tr></table></figure>
</li>
<li><p>创建两个按钮：<br>mainwidget.h中添加代码：</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">private</span>:</span><br><span class="line">    QPushButton b1;</span><br><span class="line">    QPushButton *b2;</span><br></pre></td></tr></table></figure>
</li>
</ol>
<p>mainwidget.cpp中添加：<br><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">b1.setParent(<span class="keyword">this</span>);</span><br><span class="line">b1.setText((<span class="string">"close"</span>);</span><br><span class="line">b1.move(<span class="number">100</span>,<span class="number">100</span>);</span><br><span class="line"></span><br><span class="line">b2=<span class="keyword">new</span> QPushButton(<span class="keyword">this</span>);<span class="comment">//b2是指针，要分配存储空间</span></span><br><span class="line">b2-&gt;setText(<span class="string">"abc"</span>);</span><br></pre></td></tr></table></figure></p>
<h3 id="connect函数"><a href="#connect函数" class="headerlink" title="connect函数"></a>connect函数</h3><p>用法：<br><strong>connect( 信号发出者， &amp;发出者类型::信号名字，接受者(this)，&amp;接受者类名::槽函数名字)</strong><br>说明：</p>
<ul>
<li>信号发送者和接受者均为指针类型（非指针类型添加“&amp;”符号）</li>
<li>槽函数，即信号处理函数</li>
</ul>
<p>如果是自定义的槽函数，需要注意以下几点：</p>
<ul>
<li>可以是任意的成员函数，普通全局函数，静态函数；</li>
<li><strong>槽函数需要和信号一致（返回值，参数）</strong>；</li>
<li>由于信号都是没有返回值的，所以，槽函数一定没有返回值；</li>
</ul>
<ol>
<li><p>添加b1的槽函数<br>MainWidget.cpp中的构造函数中继续添加：</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">connect(&amp;b1,&amp;QPushButton::pressed,<span class="keyword">this</span>,&amp;MainWidget::close);</span><br></pre></td></tr></table></figure>
<p>代码实现点击b1按钮，主窗口关闭。</p>
</li>
<li><p>添加b2的槽函数<br>在mainwidget.h中添加<strong>自定义公有槽函数</strong>成员：</p>
</li>
</ol>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">myslot</span><span class="params">()</span></span>;</span><br></pre></td></tr></table></figure>
<p>MainWidget.cpp中的构造函数中添加：<br><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//b2本来就是指针（地址），不需添加地址符</span></span><br><span class="line">connect(b2,&amp;QPushButton::released,<span class="keyword">this</span>,&amp;MainWidget::myslot);</span><br><span class="line">connect(b2,&amp;QPushButton::released,&amp;b1,&amp;QPushButton::hide);</span><br></pre></td></tr></table></figure></p>
<p>MainWidget.cpp中添加函数定义：<br><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">void</span> MainWidget::myslot()&#123;</span><br><span class="line">    b2-&gt;setText(<span class="string">"123"</span>);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>  以上代码实现释放b2按钮，主窗口实现自定义槽函数，修改按钮b2的内容，b1按钮实现按钮隐藏。</p>
<h3 id="创建新窗口"><a href="#创建新窗口" class="headerlink" title="创建新窗口"></a>创建新窗口</h3><p>  头文件右击添加新文件，选择c++ class,类名为subwidget,基类为Widget。</p>
<ol>
<li>在主窗口的头文件中添加按钮a3,源文件中添加代码：</li>
</ol>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">b3.setParent(<span class="keyword">this</span>);</span><br><span class="line">b3.setText(<span class="string">"切换到父窗口"</span>);</span><br><span class="line">b3.move(<span class="number">50</span>,<span class="number">50</span>);</span><br><span class="line"></span><br><span class="line"> resize(<span class="number">300</span>,<span class="number">400</span>);<span class="comment">//窗口大小设置</span></span><br></pre></td></tr></table></figure>
<ol start="2">
<li>在子窗口文件头文件添加按钮成员b,源文件中添加代码：<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">this</span>-&gt;setWindowTitle(<span class="string">"我是小弟"</span>);</span><br><span class="line">b.setParent(<span class="keyword">this</span>);</span><br><span class="line">b.setText(<span class="string">"切换到主窗口"</span>);</span><br><span class="line"></span><br><span class="line">resize(<span class="number">300</span>,<span class="number">400</span>);</span><br></pre></td></tr></table></figure>
</li>
</ol>
<h3 id="切换槽函数"><a href="#切换槽函数" class="headerlink" title="切换槽函数"></a>切换槽函数</h3><ol>
<li>主窗口切换为子窗口。</li>
</ol>
<p>① 头文件中添加公有函数成员：<br><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">changeWin</span><span class="params">()</span></span>;<span class="comment">//切换窗口</span></span><br></pre></td></tr></table></figure></p>
<p>② 添加槽函数连接和定义：<br><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//构造函数中添加</span></span><br><span class="line">    connect(&amp;b3,&amp;QPushButton::released,<span class="keyword">this</span>,&amp;MainWidget::changeWin);</span><br><span class="line">    </span><br><span class="line"><span class="comment">//函数定义</span></span><br><span class="line"><span class="keyword">void</span> MainWidget::changeWin()&#123;</span><br><span class="line">    </span><br><span class="line">    <span class="comment">//子窗口显示</span></span><br><span class="line">    w.show();</span><br><span class="line">    <span class="comment">//主窗口隐藏</span></span><br><span class="line">    <span class="keyword">this</span>-&gt;hide();</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<ol start="2">
<li>子窗口切换为主窗口</li>
</ol>
<p>子窗口发送信号</p>
<ul>
<li>信号必须有signals关键字</li>
<li>信号没有返回值，但可以有参数</li>
<li>信号就是函数的声明，只需声明，无需定义</li>
<li>使用 emit mysignel();</li>
<li>信号可以重载</li>
</ul>
<p>① 在子窗口头文件中添加公有信号处理函数和信号成员</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">sendSlot</span><span class="params">()</span></span>;</span><br><span class="line"></span><br><span class="line">signals:</span><br><span class="line">    <span class="function"><span class="keyword">void</span> <span class="title">mysignal</span><span class="params">()</span></span>;</span><br></pre></td></tr></table></figure>
<p>② 在子窗口源文件中添加连接及其信号处理函数：<br><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//子窗口构造函数中添加</span></span><br><span class="line">    connect(&amp;b,&amp;QPushButton::clicked,<span class="keyword">this</span>,&amp;subWidget::sendSlot);</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">void</span> subWidget::sendSlot()&#123;</span><br><span class="line">    <span class="function">emit <span class="title">mysignal</span><span class="params">()</span></span>;<span class="comment">//发送信号</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>③ 在主窗口中添加处理信号函数：</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">dealsubSignal</span><span class="params">()</span></span>;</span><br></pre></td></tr></table></figure>
<p>④ 在主窗口的源文件中创建信号和槽函数的连接 和处理函数的定义<br><figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">    <span class="comment">//处理主窗口信号</span></span><br><span class="line">    connect(&amp;w,&amp;subWidget::mysignal,<span class="keyword">this</span>,&amp;MainWidget::dealsubSignal);</span><br><span class="line">    </span><br><span class="line">    <span class="keyword">void</span> MainWidget::dealsubSignal()&#123;</span><br><span class="line">    <span class="comment">//子窗口隐藏</span></span><br><span class="line">    w.hide();</span><br><span class="line">    <span class="comment">//主窗口显示</span></span><br><span class="line">    <span class="keyword">this</span>-&gt;show();</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>这里<strong>解释</strong>一下：其实这里不是在子窗口中响应子窗口按钮发出的信号，而是通过主窗口来处理信号，只是他的信号发送方是子窗口，但要通过按钮实现，所以按下子窗口的按钮后的调用函数，函数执行的事件是发送一个信号给主窗口，这个信号属于子窗口。主窗口在对信号进行处理。</p>
<h3 id="带参数的信号"><a href="#带参数的信号" class="headerlink" title="带参数的信号"></a>带参数的信号</h3><ol>
<li><p>在子窗口继续添加一个带参信号：</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">signals:</span><br><span class="line">    <span class="function"><span class="keyword">void</span> <span class="title">mysignal</span><span class="params">()</span></span>;</span><br><span class="line">    <span class="function"><span class="keyword">void</span> <span class="title">mysignal</span><span class="params">(<span class="keyword">int</span>,QString)</span></span>;</span><br></pre></td></tr></table></figure>
</li>
<li><p>在前面按钮按下而发送信号的函数里继续添加发送信号：</p>
</li>
</ol>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">void</span> subWidget::sendSlot()&#123;</span><br><span class="line">    <span class="function">emit <span class="title">mysignal</span><span class="params">()</span></span>;</span><br><span class="line">    <span class="function">emit <span class="title">mysignal</span><span class="params">(<span class="number">250</span>,<span class="string">"我是子窗口"</span>)</span></span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<ol start="3">
<li>在主窗口里添加信号处理函数</li>
</ol>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">dealSlot</span><span class="params">(<span class="keyword">int</span>,QString)</span></span>;</span><br></pre></td></tr></table></figure>
<ol start="4">
<li>信号连接和处理函数定义：</li>
</ol>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">	connect(&amp;w,&amp;subWidget::mysignal,<span class="keyword">this</span>,&amp;MainWidget::dealSlot);</span><br><span class="line">	</span><br><span class="line">	<span class="comment">//信号处理函数</span></span><br><span class="line">	<span class="keyword">void</span> MainWidget::dealSlot(<span class="keyword">int</span> a, QString str)&#123;</span><br><span class="line">    qDebug()&lt;&lt;a&lt;&lt;str.toUtf8().data();</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>运行程序，发现报错，这是为什么呢？<br>因为connect里的参数&amp;subWidget::mysignal不知道是含参信号函数还是不含参信号函数啊</p>
<p>解决办法有两种：<br>① 函数指针</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">void</span> (subWidget::*withnotcansignal)()=&amp;subWidget::mysignal;</span><br><span class="line"> connect(&amp;w,withnotcansignal,<span class="keyword">this</span>,&amp;MainWidget::dealsubSignal);</span><br><span class="line"></span><br><span class="line"> <span class="keyword">void</span> (subWidget::*withcansignal)(<span class="keyword">int</span>,QString)=&amp;subWidget::mysignal;</span><br><span class="line"> connect(&amp;w,withcansignal,<span class="keyword">this</span>,&amp;MainWidget::dealSlot);</span><br></pre></td></tr></table></figure>
<p>注意： 加上作用域</p>
<p>② SIGNAL和SLOT(这是qt4的信号连接，安全性不好)</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//Qt4</span></span><br><span class="line">connect(&amp;w,SIGNAL(mysignal()),<span class="keyword">this</span>,SLOT(dealsubSignal()));</span><br><span class="line">connect(&amp;w,SIGNAL(mysignal(<span class="keyword">int</span>,QString)),<span class="keyword">this</span>,SLOT(dealSlot(<span class="keyword">int</span>,QString)));</span><br></pre></td></tr></table></figure>
<h3 id="Lamda表达式直接处理信号"><a href="#Lamda表达式直接处理信号" class="headerlink" title="Lamda表达式直接处理信号"></a>Lamda表达式直接处理信号</h3><ol>
<li><p>在.pro项目文件中添加</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">CONFIG +=C++<span class="number">11</span></span><br></pre></td></tr></table></figure>
</li>
<li><p>在主窗口的源文件中继续添加</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">QPushButton *b4=<span class="keyword">new</span> QPushButton(<span class="keyword">this</span>);</span><br><span class="line">  b4-&gt;setText(<span class="string">"lambda表达式"</span>);</span><br><span class="line">  b4-&gt;move(<span class="number">150</span>,<span class="number">150</span>);</span><br><span class="line"></span><br><span class="line">  <span class="comment">//[]里面放你要对外部那些变量进行操作的变量，否则无法捕捉到，</span></span><br><span class="line">  <span class="comment">//一般最好用“=”表示外面所有局部变量、类中所有成员以值传递方式（就像拷贝一样）</span></span><br><span class="line">  <span class="comment">//()传参,如果这个函数要处理的信号有参数，那括号里需要包含所传参数</span></span><br><span class="line">  <span class="comment">//mutable允许对传入的成员进行操作，否则只读</span></span><br><span class="line">  <span class="keyword">int</span> a=<span class="number">2</span>;</span><br><span class="line">  connect(b4,&amp;QPushButton::released,</span><br><span class="line">          [=]()<span class="keyword">mutable</span></span><br><span class="line">  &#123;</span><br><span class="line">      b4-&gt;setText(<span class="string">"123"</span>);</span><br><span class="line">      a=<span class="number">11</span>;</span><br><span class="line">  &#125;);</span><br></pre></td></tr></table></figure>
</li>
</ol>
<p>Lamda表达式可以创造一个对信号直接处理的函数，不需要重声明和定义一个函数</p>
<h2 id="创建一个主窗口"><a href="#创建一个主窗口" class="headerlink" title="创建一个主窗口"></a>创建一个主窗口</h2><h3 id="创建项目-1"><a href="#创建项目-1" class="headerlink" title="创建项目"></a>创建项目</h3><p>  新建Qt Widgets应用,项目名称QWindow ,类名默认MainWindow,基类默认为QMainWindow,不勾选创建界面。<br>  mainwindow.cpp代码如下，根据代码领会，不在解释：</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">"mainwindow.h"</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;QMenuBar&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;QMenu&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;QAction&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;QDebug&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;QToolBar&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;QPushButton&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;QStatusBar&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;QLabel&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;QTextEdit&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;QDockWidget&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;QDialog&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;QMessageBox&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;QFileDialog&gt;</span></span></span><br><span class="line"></span><br><span class="line">MainWindow::MainWindow(QWidget *parent)</span><br><span class="line">    : QMainWindow(parent)</span><br><span class="line">&#123;</span><br><span class="line">    <span class="comment">//创建菜单栏</span></span><br><span class="line">    QMenuBar *mybar=menuBar();</span><br><span class="line">    <span class="comment">// 添加菜单</span></span><br><span class="line">    QMenu *pFile=mybar-&gt;addMenu(<span class="string">"文件"</span>);</span><br><span class="line"></span><br><span class="line">    <span class="comment">//  添加菜单项，添加动作</span></span><br><span class="line">    QAction *pNew=pFile-&gt;addAction(<span class="string">"新建"</span>);</span><br><span class="line">    connect(pNew,&amp;QAction::triggered,</span><br><span class="line">            [=]()</span><br><span class="line">    &#123;</span><br><span class="line">        qDebug()&lt;&lt;<span class="string">"新建被按下"</span>;</span><br><span class="line">    &#125;);</span><br><span class="line"></span><br><span class="line">    pFile-&gt;addSeparator();<span class="comment">//添加分割线</span></span><br><span class="line">    QAction *pOpen= pFile-&gt;addAction(<span class="string">"打开"</span>);</span><br><span class="line">    connect(pOpen,&amp;QAction::triggered,</span><br><span class="line">            [=]()</span><br><span class="line">    &#123;</span><br><span class="line">        qDebug()&lt;&lt;<span class="string">"打开被按下"</span>;</span><br><span class="line">    &#125;);</span><br><span class="line"></span><br><span class="line">    <span class="comment">//工具栏,菜单项的快捷方式</span></span><br><span class="line">    QToolBar *toolbar=addToolBar(<span class="string">"ToolBar"</span>);</span><br><span class="line"></span><br><span class="line">    <span class="comment">//工具栏添加快捷键</span></span><br><span class="line">    toolbar-&gt;addAction(pNew);</span><br><span class="line"></span><br><span class="line">    <span class="comment">//工具栏添加按钮控件</span></span><br><span class="line">    QPushButton *b=<span class="keyword">new</span> QPushButton(<span class="keyword">this</span>);</span><br><span class="line">    b-&gt;setText(<span class="string">"工具按钮"</span>);</span><br><span class="line">    toolbar-&gt;addWidget(b);</span><br><span class="line"></span><br><span class="line">    connect(b,&amp;QPushButton::clicked,</span><br><span class="line">            [=]()</span><br><span class="line">    &#123;</span><br><span class="line">        b-&gt;setText(<span class="string">"哈哈哈"</span>);</span><br><span class="line">    &#125;);</span><br><span class="line"></span><br><span class="line">    <span class="comment">//状态栏</span></span><br><span class="line">    QStatusBar *sBar=statusBar();</span><br><span class="line">    QLabel *label=<span class="keyword">new</span> QLabel(<span class="keyword">this</span>);</span><br><span class="line">    label-&gt;setText(<span class="string">"程序正在运行"</span>);</span><br><span class="line">    sBar-&gt;addWidget(label);</span><br><span class="line">    <span class="comment">//addWidget从左往右添加</span></span><br><span class="line">    sBar-&gt;addWidget(<span class="keyword">new</span> QLabel(<span class="string">"2"</span>,<span class="keyword">this</span>));</span><br><span class="line">    <span class="comment">//从右向左添加</span></span><br><span class="line">    sBar-&gt;addPermanentWidget(<span class="keyword">new</span> QLabel(<span class="string">"3"</span>,<span class="keyword">this</span>));</span><br><span class="line"></span><br><span class="line">    <span class="comment">//核心控件</span></span><br><span class="line">    QTextEdit *textedit=<span class="keyword">new</span> QTextEdit(<span class="keyword">this</span>);</span><br><span class="line">    setCentralWidget(textedit);</span><br><span class="line"></span><br><span class="line">    <span class="comment">//浮动窗口</span></span><br><span class="line">    QDockWidget *dockwidget=<span class="keyword">new</span> QDockWidget(<span class="keyword">this</span>);</span><br><span class="line">    addDockWidget(Qt::RightDockWidgetArea,dockwidget);</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 设置浮动窗口内容</span></span><br><span class="line">    QTextEdit *textedit1=<span class="keyword">new</span> QTextEdit(<span class="keyword">this</span>);</span><br><span class="line">    dockwidget-&gt;setWidget(textedit1);</span><br><span class="line"></span><br><span class="line">    <span class="comment">//模态与非模态对话框</span></span><br><span class="line">    setMenuBar(mybar);</span><br><span class="line">    QMenu *menu1=mybar-&gt;addMenu(<span class="string">"对话框"</span>);</span><br><span class="line">    QAction *p1=menu1-&gt;addAction(<span class="string">"模态对话框"</span>);</span><br><span class="line">    connect(p1,&amp;QAction::triggered,</span><br><span class="line">            [=]()</span><br><span class="line">    &#123;</span><br><span class="line">        QDialog dlg;</span><br><span class="line">        dlg.exec();<span class="comment">//一直运行，直到用户操作</span></span><br><span class="line">    &#125;);</span><br><span class="line"></span><br><span class="line">    QAction *p2=menu1-&gt;addAction(<span class="string">"非模态对话框"</span>);</span><br><span class="line">    connect(p2,&amp;QAction::triggered,</span><br><span class="line">            [=]()</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="comment">//这种用法导致，点击非模态对话框，对话框闪现后就被释放</span></span><br><span class="line"><span class="comment">//        QDialog dlg;</span></span><br><span class="line"><span class="comment">//        dlg.show();</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">//运行结束时才会释放，所以你点击多少次，就会创建多少个程序，</span></span><br><span class="line">        <span class="comment">//你关闭这个对话框时他分配到空间也不会释放</span></span><br><span class="line"><span class="comment">//        QDialog *dlg1=new QDialog(this);</span></span><br><span class="line"><span class="comment">//        dlg1-&gt;show();</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">//每次点击关闭内存空间就会释放</span></span><br><span class="line">        QDialog *dlg2=<span class="keyword">new</span> QDialog(<span class="keyword">this</span>);</span><br><span class="line">        dlg2-&gt;setAttribute(Qt::WA_DeleteOnClose);</span><br><span class="line">        dlg2-&gt;show();</span><br><span class="line">    &#125;);</span><br><span class="line"></span><br><span class="line">    mybar-&gt;addSeparator();<span class="comment">//添加分割线</span></span><br><span class="line"></span><br><span class="line">    QAction *p3=menu1-&gt;addAction(<span class="string">"关于对话框"</span>);</span><br><span class="line">    connect(p3,&amp;QAction::triggered,</span><br><span class="line">            [=]()</span><br><span class="line">    &#123;</span><br><span class="line">        QMessageBox::about(<span class="keyword">this</span>,<span class="string">"about"</span>,<span class="string">"关于Qt"</span>);</span><br><span class="line">    &#125;);</span><br><span class="line"></span><br><span class="line">    QAction *p4=menu1-&gt;addAction(<span class="string">"问题对话框"</span>);</span><br><span class="line">    connect(p4,&amp;QAction::triggered,</span><br><span class="line">            [=]()</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="keyword">int</span> ret=QMessageBox::question(<span class="keyword">this</span>,<span class="string">"question"</span>,</span><br><span class="line">                              <span class="string">"Are you ok?"</span>,</span><br><span class="line">                                      QMessageBox::Ok|QMessageBox::Cancel);</span><br><span class="line">        <span class="keyword">switch</span>(ret)</span><br><span class="line">        &#123;</span><br><span class="line">        <span class="keyword">case</span> QMessageBox::Ok:</span><br><span class="line">            qDebug()&lt;&lt;<span class="string">"i am ok"</span>;</span><br><span class="line">            <span class="keyword">break</span>;</span><br><span class="line">        <span class="keyword">case</span> QMessageBox::Cancel:</span><br><span class="line">            qDebug()&lt;&lt;<span class="string">"i am bad"</span>;</span><br><span class="line">            <span class="keyword">break</span>;</span><br><span class="line">        <span class="keyword">default</span>:</span><br><span class="line">            <span class="keyword">break</span>;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;);</span><br><span class="line"></span><br><span class="line">    QAction *p5=menu1-&gt;addAction(<span class="string">"文件对话框"</span>);</span><br><span class="line">    connect(p5,&amp;QAction::triggered,</span><br><span class="line">            [=]()</span><br><span class="line">    &#123;</span><br><span class="line">        QString path=QFileDialog::getOpenFileName(</span><br><span class="line">                    <span class="keyword">this</span>,</span><br><span class="line">                    <span class="string">"open"</span>,</span><br><span class="line">                    <span class="string">"../"</span>,</span><br><span class="line">                    <span class="string">"souce(*.cpp .h);;"</span></span><br><span class="line">                    <span class="string">"Text(*.text);;"</span></span><br><span class="line">                    <span class="string">"all(*.*)"</span>);</span><br><span class="line">        qDebug()&lt;&lt;path;</span><br><span class="line">    &#125;);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">MainWindow::~MainWindow()</span><br><span class="line">&#123;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>运行结果如图：</p>
<p><img src="/image/mywindow.png" alt="窗口运行结果"></p>
<h3 id="Qt样式表"><a href="#Qt样式表" class="headerlink" title="Qt样式表"></a>Qt样式表</h3><p><a href="https://www.cnblogs.com/lsgxeva/p/7816938.html" target="_blank" rel="noopener">Qt样式表</a><br><a href="https://blog.csdn.net/wang13342322203/article/details/80689748" target="_blank" rel="noopener">Qt样式表样式大全</a></p>

      
    </div>
    
    
    

    

    
      <div>
        <div style="padding: 10px 0; margin: 20px auto; width: 90%; text-align: center;">
  <div>你可以对我进行打赏哦</div>
  <button id="rewardButton" disable="enable" onclick="var qr = document.getElementById('QR'); if (qr.style.display === 'none') {qr.style.display='block';} else {qr.style.display='none'}">
    <span>打赏</span>
  </button>
  <div id="QR" style="display: none;">

    
      <div id="wechat" style="display: inline-block">
        <img id="wechat_qr" src="/images/weichat.jpg" alt="ananzhusen 微信支付">
        <p>微信支付</p>
      </div>
    

    
      <div id="alipay" style="display: inline-block">
        <img id="alipay_qr" src="/images/alipay.jpg" alt="ananzhusen 支付宝">
        <p>支付宝</p>
      </div>
    

    

  </div>
</div>

      </div>
    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/Qtcreator/" rel="tag"># Qtcreator</a>
          
            <a href="/tags/实战/" rel="tag"># 实战</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2019/05/04/QtCreator学习（3）/" rel="next" title="QtCreator学习（3）">
                <i class="fa fa-chevron-left"></i> QtCreator学习（3）
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2019/05/19/Qt实战（2）/" rel="prev" title="Qt实战（2）">
                Qt实战（2） <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
        <!-- JiaThis Button BEGIN -->
<div class="jiathis_style">
<span class="jiathis_txt">分享到：</span>
<a class="jiathis_button_fav">收藏夹</a>
<a class="jiathis_button_copy">复制网址</a>
<a class="jiathis_button_email">邮件</a>
<a class="jiathis_button_weixin">微信</a>
<a class="jiathis_button_qzone">QQ空间</a>
<a class="jiathis_button_tqq">腾讯微博</a>
<a class="jiathis_button_douban">豆瓣</a>
<a class="jiathis_button_share">一键分享</a>

<a href="http://www.jiathis.com/share?uid=2140465" class="jiathis jiathis_txt jiathis_separator jtico jtico_jiathis" target="_blank">更多</a>
<a class="jiathis_counter_style"></a>
</div>
<script type="text/javascript">
var jiathis_config={
  data_track_clickback:true,
  summary:"",
  shortUrl:false,
  hideMore:false
}
</script>
<script type="text/javascript" src="http://v3.jiathis.com/code/jia.js?uid=" charset="utf-8"></script>
<!-- JiaThis Button END -->
      
    </div>
  </div>


          </div>
          


          

  
    <div class="comments" id="comments">
    </div>
  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image" src="/images/touxiang.JPG" alt="ananzhusen">
            
              <p class="site-author-name" itemprop="name">ananzhusen</p>
              <p class="site-description motion-element" itemprop="description"></p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/archives/">
              
                  <span class="site-state-item-count">23</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                <a href="/categories/index.html">
                  <span class="site-state-item-count">13</span>
                  <span class="site-state-item-name">分类</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-tags">
                <a href="/tags/index.html">
                  <span class="site-state-item-count">43</span>
                  <span class="site-state-item-name">标签</span>
                </a>
              </div>
            

          </nav>

          

          
            <div class="links-of-author motion-element">
                
                  <span class="links-of-author-item">
                    <a href="https://github.com/ananzhusen" target="_blank" title="GitHub">
                      
                        <i class="fa fa-fw fa-github"></i>GitHub</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://weibo.com/5817162963" target="_blank" title="weibo">
                      
                        <i class="fa fa-fw fa-weibo"></i>weibo</a>
                  </span>
                
            </div>
          

          
          

          
          
            <div class="links-of-blogroll motion-element links-of-blogroll-block">
              <div class="links-of-blogroll-title">
                <i class="fa  fa-fw fa-link"></i>
                Links
              </div>
              <ul class="links-of-blogroll-list">
                
                  <li class="links-of-blogroll-item">
                    <a href="https://gitee.com/anzhusen/" title="gitee" target="_blank">gitee</a>
                  </li>
                
              </ul>
            </div>
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#Qt实战"><span class="nav-number">1.</span> <span class="nav-text">Qt实战</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#通过两个独立窗口的切换来学习信号和槽"><span class="nav-number">1.1.</span> <span class="nav-text">通过两个独立窗口的切换来学习信号和槽</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#创建项目"><span class="nav-number">1.1.1.</span> <span class="nav-text">创建项目</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#connect函数"><span class="nav-number">1.1.2.</span> <span class="nav-text">connect函数</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#创建新窗口"><span class="nav-number">1.1.3.</span> <span class="nav-text">创建新窗口</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#切换槽函数"><span class="nav-number">1.1.4.</span> <span class="nav-text">切换槽函数</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#带参数的信号"><span class="nav-number">1.1.5.</span> <span class="nav-text">带参数的信号</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Lamda表达式直接处理信号"><span class="nav-number">1.1.6.</span> <span class="nav-text">Lamda表达式直接处理信号</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#创建一个主窗口"><span class="nav-number">1.2.</span> <span class="nav-text">创建一个主窗口</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#创建项目-1"><span class="nav-number">1.2.1.</span> <span class="nav-text">创建项目</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Qt样式表"><span class="nav-number">1.2.2.</span> <span class="nav-text">Qt样式表</span></a></li></ol></li></ol></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2021</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">ananzhusen</span>

  
</div>

<!-- 
  <div class="powered-by">由 <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a> 强力驱动</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Gemini</a> v5.1.4</div>
 -->



        
<div class="busuanzi-count">
  <script async src="https://dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js"></script>

  
    <span class="site-uv">
      本站访客数 <i class="fa fa-user"></i>
      <span class="busuanzi-value" id="busuanzi_value_site_uv"></span>
      人次
    </span>
  

  
    <span class="site-pv">
      本站访问总量 <i class="fa fa-eye"></i>
      <span class="busuanzi-value" id="busuanzi_value_site_pv"></span>
      次
    </span>
  
</div>








        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  


  











  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  

  
  
    <script type="text/javascript" src="/lib/canvas-nest/canvas-nest.min.js"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.4"></script>



  
  


  <script type="text/javascript" src="/js/src/affix.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/schemes/pisces.js?v=5.1.4"></script>



  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  










  <script src="//cdn1.lncld.net/static/js/3.0.4/av-min.js"></script>
  <script src="//unpkg.com/valine/dist/Valine.min.js"></script>
  
  <script type="text/javascript">
    var GUEST = ['nick','mail','link'];
    var guest = 'nick,mail,link';
    guest = guest.split(',').filter(item=>{
      return GUEST.indexOf(item)>-1;
    });
    new Valine({
        el: '#comments' ,
        verify: false,
        notify: false,
        appId: 'e69lyQSoCtoaLGljT3kXLBEQ-gzGzoHsz',
        appKey: 'pRDcFfyQnO4jQWxElUFupJ2D',
        placeholder: 'Just go go',
        avatar:'mm',
        guest_info:guest,
        pageSize:'10' || 10,
    });
  </script>



  

  <script type="text/javascript">
    // Popup Window;
    var isfetched = false;
    var isXml = true;
    // Search DB path;
    var search_path = "search.xml";
    if (search_path.length === 0) {
      search_path = "search.xml";
    } else if (/json$/i.test(search_path)) {
      isXml = false;
    }
    var path = "/" + search_path;
    // monitor main search box;

    var onPopupClose = function (e) {
      $('.popup').hide();
      $('#local-search-input').val('');
      $('.search-result-list').remove();
      $('#no-result').remove();
      $(".local-search-pop-overlay").remove();
      $('body').css('overflow', '');
    }

    function proceedsearch() {
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay"></div>')
        .css('overflow', 'hidden');
      $('.search-popup-overlay').click(onPopupClose);
      $('.popup').toggle();
      var $localSearchInput = $('#local-search-input');
      $localSearchInput.attr("autocapitalize", "none");
      $localSearchInput.attr("autocorrect", "off");
      $localSearchInput.focus();
    }

    // search function;
    var searchFunc = function(path, search_id, content_id) {
      'use strict';

      // start loading animation
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay">' +
          '<div id="search-loading-icon">' +
          '<i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>' +
          '</div>' +
          '</div>')
        .css('overflow', 'hidden');
      $("#search-loading-icon").css('margin', '20% auto 0 auto').css('text-align', 'center');

      $.ajax({
        url: path,
        dataType: isXml ? "xml" : "json",
        async: true,
        success: function(res) {
          // get the contents from search data
          isfetched = true;
          $('.popup').detach().appendTo('.header-inner');
          var datas = isXml ? $("entry", res).map(function() {
            return {
              title: $("title", this).text(),
              content: $("content",this).text(),
              url: $("url" , this).text()
            };
          }).get() : res;
          var input = document.getElementById(search_id);
          var resultContent = document.getElementById(content_id);
          var inputEventFunction = function() {
            var searchText = input.value.trim().toLowerCase();
            var keywords = searchText.split(/[\s\-]+/);
            if (keywords.length > 1) {
              keywords.push(searchText);
            }
            var resultItems = [];
            if (searchText.length > 0) {
              // perform local searching
              datas.forEach(function(data) {
                var isMatch = false;
                var hitCount = 0;
                var searchTextCount = 0;
                var title = data.title.trim();
                var titleInLowerCase = title.toLowerCase();
                var content = data.content.trim().replace(/<[^>]+>/g,"");
                var contentInLowerCase = content.toLowerCase();
                var articleUrl = decodeURIComponent(data.url);
                var indexOfTitle = [];
                var indexOfContent = [];
                // only match articles with not empty titles
                if(title != '') {
                  keywords.forEach(function(keyword) {
                    function getIndexByWord(word, text, caseSensitive) {
                      var wordLen = word.length;
                      if (wordLen === 0) {
                        return [];
                      }
                      var startPosition = 0, position = [], index = [];
                      if (!caseSensitive) {
                        text = text.toLowerCase();
                        word = word.toLowerCase();
                      }
                      while ((position = text.indexOf(word, startPosition)) > -1) {
                        index.push({position: position, word: word});
                        startPosition = position + wordLen;
                      }
                      return index;
                    }

                    indexOfTitle = indexOfTitle.concat(getIndexByWord(keyword, titleInLowerCase, false));
                    indexOfContent = indexOfContent.concat(getIndexByWord(keyword, contentInLowerCase, false));
                  });
                  if (indexOfTitle.length > 0 || indexOfContent.length > 0) {
                    isMatch = true;
                    hitCount = indexOfTitle.length + indexOfContent.length;
                  }
                }

                // show search results

                if (isMatch) {
                  // sort index by position of keyword

                  [indexOfTitle, indexOfContent].forEach(function (index) {
                    index.sort(function (itemLeft, itemRight) {
                      if (itemRight.position !== itemLeft.position) {
                        return itemRight.position - itemLeft.position;
                      } else {
                        return itemLeft.word.length - itemRight.word.length;
                      }
                    });
                  });

                  // merge hits into slices

                  function mergeIntoSlice(text, start, end, index) {
                    var item = index[index.length - 1];
                    var position = item.position;
                    var word = item.word;
                    var hits = [];
                    var searchTextCountInSlice = 0;
                    while (position + word.length <= end && index.length != 0) {
                      if (word === searchText) {
                        searchTextCountInSlice++;
                      }
                      hits.push({position: position, length: word.length});
                      var wordEnd = position + word.length;

                      // move to next position of hit

                      index.pop();
                      while (index.length != 0) {
                        item = index[index.length - 1];
                        position = item.position;
                        word = item.word;
                        if (wordEnd > position) {
                          index.pop();
                        } else {
                          break;
                        }
                      }
                    }
                    searchTextCount += searchTextCountInSlice;
                    return {
                      hits: hits,
                      start: start,
                      end: end,
                      searchTextCount: searchTextCountInSlice
                    };
                  }

                  var slicesOfTitle = [];
                  if (indexOfTitle.length != 0) {
                    slicesOfTitle.push(mergeIntoSlice(title, 0, title.length, indexOfTitle));
                  }

                  var slicesOfContent = [];
                  while (indexOfContent.length != 0) {
                    var item = indexOfContent[indexOfContent.length - 1];
                    var position = item.position;
                    var word = item.word;
                    // cut out 100 characters
                    var start = position - 20;
                    var end = position + 80;
                    if(start < 0){
                      start = 0;
                    }
                    if (end < position + word.length) {
                      end = position + word.length;
                    }
                    if(end > content.length){
                      end = content.length;
                    }
                    slicesOfContent.push(mergeIntoSlice(content, start, end, indexOfContent));
                  }

                  // sort slices in content by search text's count and hits' count

                  slicesOfContent.sort(function (sliceLeft, sliceRight) {
                    if (sliceLeft.searchTextCount !== sliceRight.searchTextCount) {
                      return sliceRight.searchTextCount - sliceLeft.searchTextCount;
                    } else if (sliceLeft.hits.length !== sliceRight.hits.length) {
                      return sliceRight.hits.length - sliceLeft.hits.length;
                    } else {
                      return sliceLeft.start - sliceRight.start;
                    }
                  });

                  // select top N slices in content

                  var upperBound = parseInt('1');
                  if (upperBound >= 0) {
                    slicesOfContent = slicesOfContent.slice(0, upperBound);
                  }

                  // highlight title and content

                  function highlightKeyword(text, slice) {
                    var result = '';
                    var prevEnd = slice.start;
                    slice.hits.forEach(function (hit) {
                      result += text.substring(prevEnd, hit.position);
                      var end = hit.position + hit.length;
                      result += '<b class="search-keyword">' + text.substring(hit.position, end) + '</b>';
                      prevEnd = end;
                    });
                    result += text.substring(prevEnd, slice.end);
                    return result;
                  }

                  var resultItem = '';

                  if (slicesOfTitle.length != 0) {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + highlightKeyword(title, slicesOfTitle[0]) + "</a>";
                  } else {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + title + "</a>";
                  }

                  slicesOfContent.forEach(function (slice) {
                    resultItem += "<a href='" + articleUrl + "'>" +
                      "<p class=\"search-result\">" + highlightKeyword(content, slice) +
                      "...</p>" + "</a>";
                  });

                  resultItem += "</li>";
                  resultItems.push({
                    item: resultItem,
                    searchTextCount: searchTextCount,
                    hitCount: hitCount,
                    id: resultItems.length
                  });
                }
              })
            };
            if (keywords.length === 1 && keywords[0] === "") {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-search fa-5x" /></div>'
            } else if (resultItems.length === 0) {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-frown-o fa-5x" /></div>'
            } else {
              resultItems.sort(function (resultLeft, resultRight) {
                if (resultLeft.searchTextCount !== resultRight.searchTextCount) {
                  return resultRight.searchTextCount - resultLeft.searchTextCount;
                } else if (resultLeft.hitCount !== resultRight.hitCount) {
                  return resultRight.hitCount - resultLeft.hitCount;
                } else {
                  return resultRight.id - resultLeft.id;
                }
              });
              var searchResultList = '<ul class=\"search-result-list\">';
              resultItems.forEach(function (result) {
                searchResultList += result.item;
              })
              searchResultList += "</ul>";
              resultContent.innerHTML = searchResultList;
            }
          }

          if ('auto' === 'auto') {
            input.addEventListener('input', inputEventFunction);
          } else {
            $('.search-icon').click(inputEventFunction);
            input.addEventListener('keypress', function (event) {
              if (event.keyCode === 13) {
                inputEventFunction();
              }
            });
          }

          // remove loading animation
          $(".local-search-pop-overlay").remove();
          $('body').css('overflow', '');

          proceedsearch();
        }
      });
    }

    // handle and trigger popup window;
    $('.popup-trigger').click(function(e) {
      e.stopPropagation();
      if (isfetched === false) {
        searchFunc(path, 'local-search-input', 'local-search-result');
      } else {
        proceedsearch();
      };
    });

    $('.popup-btn-close').click(onPopupClose);
    $('.popup').click(function(e){
      e.stopPropagation();
    });
    $(document).on('keyup', function (event) {
      var shouldDismissSearchPopup = event.which === 27 &&
        $('.search-popup').is(':visible');
      if (shouldDismissSearchPopup) {
        onPopupClose();
      }
    });
  </script>





  

  

  

  
  

  

  

  


  
   <canvas class="fireworks" style="position: fixed;left: 0;top: 0;z-index: 1; pointer-events: none;"></canvas> 
   <script type="text/javascript" src="//cdn.bootcss.com/animejs/2.2.0/anime.min.js"></script> 
   <script type="text/javascript" src="/js/src/fireworks.js"></script>
  

</body>
</html>
